//
//  RegistViewController.m
//  WhatsChat
//
//  Created by 彭琦峰 on 16/3/22.
//  Copyright © 2016年 tanchen. All rights reserved.
//

#import "RegistViewController.h"
#import <AVOSCloud/AVOSCloud.h>
#import "LoginViewController.h"

@interface RegistViewController ()

@end

@implementation RegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
//注册按钮
- (IBAction)registAction:(UIButton *)sender {
    
    AVUser *user = [AVUser user];// 新建 AVUser 对象实例
    user.username = self.userNameTextField.text;// 设置用户名
    user.password =  self.passwordTextField.text;// 设置密码
    user.email = self.EmailTextField.text;// 设置邮箱
    
    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            
            NSLog(@"注册成功");
            
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"注册成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                [self dismissViewControllerAnimated:YES completion:nil];
                //传值(用户名)
                [[NSUserDefaults standardUserDefaults] setObject:self.userNameTextField.text forKey:@"userName"];
                [[NSUserDefaults standardUserDefaults] synchronize];
                
            }];
            
            [alert addAction:alertAction];
            
            [self presentViewController:alert animated:YES completion:nil];
            
            NSLog(@"%@",user.username);
            
        } else {
            
            NSLog(@"注册失败");
            //判断用户名，密码，邮箱是否为空
            if ([self.userNameTextField.text isEqualToString:@""]) {
                
                // 失败的原因可能有多种，常见的是用户名已经存在。
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"用户名不能为空" message:nil preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil];
                
                [alert addAction:alertAction];
                
                [self presentViewController:alert animated:YES completion:nil];
                
            }else if ([self.passwordTextField.text isEqualToString:@""]) {
                
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"密码不能为空" message:nil preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil];
                
                [alert addAction:alertAction];
                
                [self presentViewController:alert animated:YES completion:nil];
            
            }else if ([self.EmailTextField.text isEqualToString:@""]){
            
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"邮箱不能为空" message:nil preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil];
                
                [alert addAction:alertAction];
                
                [self presentViewController:alert animated:YES completion:nil];
            
            }
        }
    }];
}

//取消按钮
- (IBAction)cancelAction:(UIButton *)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
